package com.briup.cms.web.controller;


import com.briup.cms.aop.Logging;
import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "资讯信息相关接口")
@RequestMapping("/auth/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @ApiOperation("根据id查询咨讯信息")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query")
    @GetMapping("/findById")
    public Message selectByPrimaryKey(Integer id){
        Article article=articleService.selectByPrimaryKey(id);
        return MessageUtil.success(article);
    }

    @Logging
    @ApiOperation("保存或更新消息信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", paramType = "query"),
            @ApiImplicitParam(name = "articleTitle", paramType = "query"),
            @ApiImplicitParam(name = "articleContent", paramType = "query"),
            @ApiImplicitParam(name = "articlePublishTime", paramType = "query"),
            @ApiImplicitParam(name = "articleReadTimes", paramType = "query"),
            @ApiImplicitParam(name = "articleStatus", paramType = "query"),
            @ApiImplicitParam(name = "articleThumpUp", paramType = "query"),
            @ApiImplicitParam(name = "articleCover", paramType = "query"),
            @ApiImplicitParam(name = "articleUserId", paramType = "query"),
            @ApiImplicitParam(name = "articleCategoryId", paramType = "query"),
    })
    @PostMapping("/saveOrUpdate")
    public Message insert(Article article){
        Article article1 = articleService.saveOrUpdate(article);
        return MessageUtil.success(article1);
    }

    @Logging
    @ApiOperation("通过id删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "query")
    @DeleteMapping("/deleteById")
    public Message deleteByPrimaryKey(Integer id){
        articleService.delete(id);
        return MessageUtil.success("已删除编号为"+ id + "的咨讯");
    }

    @Logging
    @ApiOperation("修改咨讯状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态,0-待审核，1-不通过，2-通过，3-推荐", required = true, paramType = "query"),
    })
    @PutMapping("/changeStatus")
    public Message changeStatus(Integer id, Integer status){
        articleService.changeStatus(id, status);
        return MessageUtil.success("审核成功");
    }

    @ApiOperation("分页查询所有咨讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, paramType = "query"),
            @ApiImplicitParam(name = "title", value = "资讯标题", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "咨询状态",paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "栏目ID", paramType = "query"),
    })
    @GetMapping("/page/queryWithCategoryAndUser")
    public Message queryArticleWithCategoryAndUser(Integer pageNum, Integer pageSize, String title, Integer status, Integer categoryId){
        PageInfo<ArticleExtend> info = articleService.pageQueryArticleWithCategoryAndUser(pageNum, pageSize, title, status, categoryId);
        return MessageUtil.success(info);
    }

    @ApiOperation("分页获取所有推荐咨讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, paramType = "query"),
    })
    @GetMapping("/page/query/recommend")
    public Message queryArticleRecommended(int page, int pageSize){
        PageInfo<ArticleExtend> info = articleService.pageQueryArticleRecommended(page, pageSize);
        return MessageUtil.success(info);
    }

    @ApiOperation("以栏目ID删除咨讯")
    @ApiImplicitParam(name = "categoryId", value = "栏目ID")
    @DeleteMapping("deleteArticleByCategoryId")
    public Message deleteArticleByCategoryId(int categoryId){
        articleService.deleteArticleByCategoryId(categoryId);
        return MessageUtil.success("删除成功");
    }


}
